package com.group3.admin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.group3.admin.dao.IAMerchantDao;
import com.group3.admin.util.DbUtils;
import com.group3.entity.AMerchant;

public class AMerchantDaoImpl implements IAMerchantDao{

	@Override
	public AMerchant insertMerchant(AMerchant merchant) {
		String sql = "insert into AMERCHANT values(?,?,?,?,?,?,?,?)";
		Connection conn = DbUtils.getConnection();
		PreparedStatement ps;
		AMerchant aMerchant = null;
		try {
//			aMerchant = new AMerchant();
			ps = conn.prepareStatement(sql);
			ps.setString(1, merchant.getmId());
			ps.setString(2, merchant.getmName());
			ps.setString(3, merchant.getAddress());
			ps.setString(4, merchant.getmImage());
			ps.setString(5, merchant.getIdCard());
			ps.setString(6, merchant.getType());
			ps.setString(7, merchant.getReason());
			ps.setString(8, merchant.getStatus());
			int lines = ps.executeUpdate();
			if(lines!=0){
				aMerchant = merchant;
			}
			DbUtils.free(conn, ps, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return aMerchant;
	}

	@Override
	public AMerchant queryMerchantByMId(String mid) {
		String sql = "select * from AMERCHANT where m_id=?";
		AMerchant aMerchant = null;
		Connection conn = DbUtils.getConnection();
		PreparedStatement ps; 
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,mid);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				aMerchant = new AMerchant();
				aMerchant.setmId(rs.getString(1));
				aMerchant.setmName(rs.getString(2));
				aMerchant.setAddress(rs.getString(3));
				aMerchant.setmImage(rs.getString(4));
				aMerchant.setIdCard(rs.getString(5));
				aMerchant.setType(rs.getString(6));
				aMerchant.setReason(rs.getString(7));
				aMerchant.setStatus(rs.getString(8));
				
			}
			DbUtils.free(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return aMerchant;
	}

	@Override
	public List<AMerchant> queryByType(String type) {
		List<AMerchant> list = new ArrayList<>();
		String sql = "select * from AMERCHANT where type=?";
		AMerchant aMerchant = null;
		Connection conn = DbUtils.getConnection();
		PreparedStatement ps; 
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,type);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				aMerchant = new AMerchant();
				aMerchant.setmId(rs.getString(1));
				aMerchant.setmName(rs.getString(2));
				aMerchant.setAddress(rs.getString(3));
				aMerchant.setmImage(rs.getString(4));
				aMerchant.setIdCard(rs.getString(5));
				aMerchant.setType(rs.getString(6));
				aMerchant.setReason(rs.getString(7));
				aMerchant.setStatus(rs.getString(8));
				list.add(aMerchant);
			}
			DbUtils.free(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public AMerchant updateMerchant(AMerchant aMerchant) {
		String sql = "update amerchant set type=?,reason=?, status = ? where m_id=?";
		int lines = 0;
		Connection conn = DbUtils.getConnection();
		AMerchant aMerchant2 = null;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, aMerchant.getType());
			ps.setString(2, aMerchant.getReason());
			ps.setString(3, aMerchant.getStatus());
			ps.setString(4, aMerchant.getmId());			
			lines = ps.executeUpdate();
			if(lines!=0){
				aMerchant2 = aMerchant;
			}
			DbUtils.free(conn, ps, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return aMerchant2;
	}

	@Override
	public List<AMerchant> queryByStatus(String status) {
		List<AMerchant> list = new ArrayList<>();
		String sql = "select * from amerchant where status=?";
		AMerchant aMerchant = null;
		Connection conn = DbUtils.getConnection();
		PreparedStatement ps; 
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,status);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				aMerchant = new AMerchant();
				aMerchant.setmId(rs.getString(1));
				aMerchant.setmName(rs.getString(2));
				aMerchant.setAddress(rs.getString(3));
				aMerchant.setmImage(rs.getString(4));
				aMerchant.setIdCard(rs.getString(5));
				aMerchant.setType(rs.getString(6));
				aMerchant.setReason(rs.getString(7));
				aMerchant.setStatus(rs.getString(8));
				list.add(aMerchant);
			}
			DbUtils.free(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public AMerchant updateStatusByType(AMerchant aMerchant) {
		String sql = "update amerchant set status=? where m_id=?";
		int lines = 0;
		Connection conn = DbUtils.getConnection();
		AMerchant aMerchant2 = null;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, aMerchant.getStatus());
			ps.setString(2, aMerchant.getmId());
			lines = ps.executeUpdate();
			if(lines!=0){
				aMerchant2 = aMerchant;
			}
			DbUtils.free(conn, ps, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return aMerchant2;
	}

	@Override
	public List<AMerchant> queryAllMerchant() {
		List<AMerchant> list = new ArrayList<>();
		String sql = "select * from amerchant";
		AMerchant aMerchant = null;
		Connection conn = DbUtils.getConnection();
		PreparedStatement ps; 
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				aMerchant = new AMerchant();
				aMerchant.setmId(rs.getString(1));
				aMerchant.setmName(rs.getString(2));
				aMerchant.setAddress(rs.getString(3));
				aMerchant.setmImage(rs.getString(4));
				aMerchant.setIdCard(rs.getString(5));
				aMerchant.setType(rs.getString(6));
				aMerchant.setReason(rs.getString(7));
				aMerchant.setStatus(rs.getString(8));
				list.add(aMerchant);
			}
			DbUtils.free(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<String> midListQueryByType() {
		Connection connection = DbUtils.getConnection();
		String sql = "select m_id from amerchant where type =? and status =?";
		List<String> midList = new ArrayList<>();
		try {
			
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, "ASSESSED");
			ps.setString(2, "WHITE");
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				midList.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return midList;
	}

	@Override
	public AMerchant updateResubmitAMerchant(AMerchant aMerchant) {
		String sql = "update amerchant set m_name=?,m_address=?,m_image=?,m_idcard=? where m_id=?";
		int lines = 0;
		Connection conn = DbUtils.getConnection();
		AMerchant aMerchant2 = null;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, aMerchant.getmName());
			ps.setString(2, aMerchant.getAddress());
			ps.setString(3, aMerchant.getmImage());
			ps.setString(4, aMerchant.getIdCard());
			ps.setString(5, aMerchant.getmId());
			lines = ps.executeUpdate();
			if(lines!=0){
				aMerchant2 = aMerchant;
			}
			DbUtils.free(conn, ps, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return aMerchant2;
	}
}
